<?php 

// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php'); 

// Set the page title and include the HTML header.
$page_title = 'Newnham College Associates: Admin: Inactive';
$site_section = 'Admin';
include ('./includes/header.html');
include ('./includes/fetch-user.inc.php');
include ('./includes/admin.inc.php');

// is the user logged in?
if (!isset($_SESSION['user_level'])) {
  user_redirect('/login.php');
  exit(); // Quit the script.
} 
// is the user an admin?
if ($_SESSION['user_level'] < 10) {
  user_redirect('/index.php');
  exit(); // Quit the script.
} 

 // they are logged in, so we can continue
require_once ('./mysql_connect.php'); // Connect to the database.


if (isset($_POST['submitted'])) { // Handle the  submitted form.
  if (isset($_POST['aselected'])) {
    $vals = $_POST['aselected'];
    // change active to inactive
    foreach ($vals as $val) { 
      $query = "UPDATE user_details SET is_active = 0 where user_id = $val";
      $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
    }
  }
  if (isset($_POST['iselected'])) {
    $vals = $_POST['iselected'];
    // change active to inactive
    foreach ($vals as $val) { 
      $query = "UPDATE user_details SET is_active = 1 where user_id = $val";
      $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
    }
  }
  
}
$active_user_results = fetch_active_user_results(TRUE);
while ($row=mysql_fetch_array($active_user_results)) { 
  $active_users[] = $row;
}
$inactive_user_results = fetch_active_user_results(FALSE);
$inactive_users = NULL;
while ($row=mysql_fetch_array($inactive_user_results)) { 
  $inactive_users[] = $row;
}


mysql_close(); // Close the database connection.

?>

<h1>Active/Inactive associates</h1>

<p>Inactive associates appear on no lists and are ignored on all other pages on this site. Making an associate inactive is equivalent to removing her from the system (except that her details are retained).</p>

<form action="inactive.php" method="post">

<h2>Active users</h2>
<table>

<?php
    foreach ($active_users as $user) {
      echo "<tr><td><a href=\"assoc.php?assoc={$user['user_id']}&do=admin\">{$user['first_name']}&nbsp;{$user['last_name']}</a></td>";
      echo "<td>";
      echo "<input type=\"checkbox\" name=\"aselected[]\" value=\"{$user['user_id']}\" />";
      echo "</td>";
      echo "</tr>\n";
    }
?>
</table>
<h2>Inactive users</h2>
<table>

<?php
if (count($inactive_users) > 0) {
    foreach ($inactive_users as $user) {
      echo "<tr><td><a href=\"assoc.php?assoc={$user['user_id']}&do=admin\">{$user['first_name']}&nbsp;{$user['last_name']}</a></td>";
      echo "<td>";
      echo "<input type=\"checkbox\" name=\"iselected[]\" value=\"{$user['user_id']}\" />";
      echo "</td>";
      echo "</tr>\n";
    }
}
?>
</table>

<div align="center"  style="margin-top: 1em;"><input type="submit" name="submit" value="Change selected" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

       

<?php // Include the HTML footer file.
include ('./includes/footer.html');
?>